#!/usr/bin/env python
from os import path
import pylab
import sys

# Collects scores, sorts them, and then graphs them.
# **NOTE: You'd better be sure no duplicate networks occur..
# Syntax: python collectScores.py <in file 1> <in file 2> ...

# We got arguments, right..?
if len(sys.argv[1:]) == 0:
  print "Syntax is: python graphScores.py <input file 1> <input file 2> ... <input file n>";
  sys.exit(1);

# Check input files to make sure they exist. 
for file in sys.argv[1:]:
  if not path.isfile(file):
    raise Exception, "File not found: " + file;
  
# Store scores here.
scores = list();

# Open each file, get the scores, etc.
for file in sys.argv[1:]:
  f = open(file,"r");
  f.readline(); # kill header
  for line in f:
    items = line.split("\t");
    if len(items) == 3 and items[2].strip() != "":
      score = float(items[2].strip());
      scores.append(score);
  
  f.close();

# Sort them.
scores = sorted(scores,reverse=True);

# Graph them.
xmin = 0;
xmax = len(scores);
ymin = scores[-1]-1;
ymax = scores[0]+1;

pylab.plot(scores,'ro');
pylab.axis([xmin,xmax,ymin,ymax]);
print "xrange: " + str((xmin,xmax));
print "yrange: " + str((ymin,ymax));
pylab.xlabel('sorted network');
pylab.ylabel('log P(M|D)');
pylab.title('Posterior Distribution');
pylab.show();